//
//  CommunicationVC.m
//  SYPerfectDemo
//
//  Created by Lssyyy on 2019/3/28.
//  Copyright © 2019年 Lssyyy. All rights reserved.
//

#import "CommunicationVC.h"
#import "ServiceModel.h"
#import "ServiceCell.h"
@interface CommunicationVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic,strong) UITableView *tableView;
@end

@implementation CommunicationVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addUI];
    [self setupDataWithCount:30];
}

- (void)addUI{
    [self.view addSubview:self.tableView];
}
- (void)setupDataWithCount:(NSInteger)count{
    for (int i = 0; i < count; i++) {
        ServiceModel *model = [ServiceModel new];
        model.messageType = arc4random_uniform(2);
        model.IF_DOWN = [self getRandomNumber:1 to:2];
        if (model.messageType) {
            model.iconName = @"Shake_icon_people";
        } else {
            model.iconName = @"Shake_icon_peopleHL";
        }
        model.CSC_NAME = [self getStr];
        [self.dataArray addObject:model];
    }
    [self.tableView reloadData];
}
- (NSString *)getStr{
    
    NSMutableArray *array = [NSMutableArray arrayWithObjects:@"你是谁",@"天气真好天气真好天气真好天气真好天气真好天气真好天气真好天气真好天气真好天气真好",@"星期四星期四星期四星期四星期四星期四星期四星期四星期四星期四星期四星期四星期四星期四星期四星期四星期四星期四星期四星期四",@"准备准备准备准备准备准备准备准备准备准备准备准备准备准备准备准备准备准备准备准备准备准备准备准备准备准备准备准备准备准备准备",@"大家好",@"TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT",@"测试内容\n测试内容\n测试内容\n测试内容\n测试内容\n测试内容\n测试内容\n测试内容\n测试内容\n测试内容\n测试内容\n测试内容\n测试内容\n测试内容\n测试内容\n测试内容\n测试内容\n测试内容\n测试内容\n测试内容\n测试内容\n测试内容\n测试内容\n测试内容\n测试内容\n测试内容\n测试内容\n测试内容\n测试内容\n测试内容\n测试内容\n测试内容\n测试内容\n测试内容\n测试内容\n", nil];
    
    int i = [self getRandomNumber:1 to:array.count];
    NSString *str = array[i-1];
    return str;
}

#pragma mark - tableview delegate and datasource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ServiceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ServiceCell"];
    cell.model = self.dataArray[indexPath.row];
    __weak typeof(self) weakSelf = self;
//    [cell setDidSelectLinkTextOperationBlock:^(NSString *link, MLEmojiLabelLinkType type) {
//        if (type == MLEmojiLabelLinkTypeURL) {
//            [weakSelf.navigationController pushViewController:[SDWebViewController webViewControllerWithUrlString:link] animated:YES];
//        }
//    }];
    
    cell.sd_tableView = tableView;
    cell.sd_indexPath = indexPath;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat h = [self.tableView cellHeightForIndexPath:indexPath model:self.dataArray[indexPath.row] keyPath:@"model" cellClass:[ServiceCell class] contentViewWidth:[UIScreen mainScreen].bounds.size.width];
    return h;
}



//1.3 抽取方法：随机整数，范围在[from, to]
-(int)getRandomNumber:(int)from to:(int)to
{
    NSLog(@"随机数：%u",(from + (arc4random() % (to - from + 1))));
    return (int)(from + (arc4random() % (to - from + 1)));
}


- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[ServiceCell class] forCellReuseIdentifier:NSStringFromClass([ServiceCell class])];
    }
    return _tableView;
}

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
@end
